using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Xml;
using System.Xml.Linq;
namespace ServiceStack
{
	public static class XLinqExtensions
	{
		[CompilerGenerated]
		[Serializable]
		private sealed class <>c
		{
			public static readonly XLinqExtensions.<>c <>9 = new XLinqExtensions.<>c();
			public static Func<XElement, string> <>9__0_0;
			public static Func<XAttribute, string> <>9__1_0;
			public static Func<XElement, bool> <>9__4_0;
			public static Func<XElement, int> <>9__7_0;
			public static Func<XElement, long> <>9__10_0;
			public static Func<XElement, decimal> <>9__13_0;
			public static Func<XElement, DateTime> <>9__16_0;
			public static Func<XElement, TimeSpan> <>9__19_0;
			public static Func<XElement, Guid> <>9__22_0;
			internal string <GetString>b__0_0(XElement x)
			{
				return x.Value;
			}
			internal string <GetStringAttributeOrDefault>b__1_0(XAttribute x)
			{
				return x.Value;
			}
			internal bool <GetBoolOrDefault>b__4_0(XElement x)
			{
				return (bool)x;
			}
			internal int <GetIntOrDefault>b__7_0(XElement x)
			{
				return (int)x;
			}
			internal long <GetLongOrDefault>b__10_0(XElement x)
			{
				return (long)x;
			}
			internal decimal <GetDecimalOrDefault>b__13_0(XElement x)
			{
				return (decimal)x;
			}
			internal DateTime <GetDateTimeOrDefault>b__16_0(XElement x)
			{
				return (DateTime)x;
			}
			internal TimeSpan <GetTimeSpanOrDefault>b__19_0(XElement x)
			{
				return (TimeSpan)x;
			}
			internal Guid <GetGuidOrDefault>b__22_0(XElement x)
			{
				return (Guid)x;
			}
		}
		public static string GetString(this XElement el, string name)
		{
			if (el != null)
			{
				Func<XElement, string> arg_24_2;
				if ((arg_24_2 = XLinqExtensions.<>c.<>9__0_0) == null)
				{
					arg_24_2 = (XLinqExtensions.<>c.<>9__0_0 = new Func<XElement, string>(XLinqExtensions.<>c.<>9.<GetString>b__0_0));
				}
				return el.GetElementValueOrDefault(name, arg_24_2);
			}
			return null;
		}
		public static string GetStringAttributeOrDefault(this XElement element, string name)
		{
			XAttribute xAttribute = element.AnyAttribute(name);
			if (xAttribute != null)
			{
				XAttribute arg_2C_0 = xAttribute;
				Func<XAttribute, string> arg_2C_2;
				if ((arg_2C_2 = XLinqExtensions.<>c.<>9__1_0) == null)
				{
					arg_2C_2 = (XLinqExtensions.<>c.<>9__1_0 = new Func<XAttribute, string>(XLinqExtensions.<>c.<>9.<GetStringAttributeOrDefault>b__1_0));
				}
				return arg_2C_0.GetAttributeValueOrDefault(name, arg_2C_2);
			}
			return null;
		}
		public static T GetAttributeValueOrDefault<T>(this XAttribute attr, string name, Func<XAttribute, T> converter)
		{
			if (converter == null)
			{
				throw new ArgumentNullException("converter");
			}
			if (attr != null && !string.IsNullOrEmpty(attr.Value))
			{
				return converter(attr);
			}
			return default(T);
		}
		public static bool GetBool(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (bool)el.GetElement(name);
		}
		public static bool GetBoolOrDefault(this XElement el, string name)
		{
			Func<XElement, bool> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__4_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__4_0 = new Func<XElement, bool>(XLinqExtensions.<>c.<>9.<GetBoolOrDefault>b__4_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static bool? GetNullableBool(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (bool?)element;
			}
			return null;
		}
		public static int GetInt(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (int)el.GetElement(name);
		}
		public static int GetIntOrDefault(this XElement el, string name)
		{
			Func<XElement, int> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__7_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__7_0 = new Func<XElement, int>(XLinqExtensions.<>c.<>9.<GetIntOrDefault>b__7_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static int? GetNullableInt(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (int?)element;
			}
			return null;
		}
		public static long GetLong(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (long)el.GetElement(name);
		}
		public static long GetLongOrDefault(this XElement el, string name)
		{
			Func<XElement, long> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__10_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__10_0 = new Func<XElement, long>(XLinqExtensions.<>c.<>9.<GetLongOrDefault>b__10_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static long? GetNullableLong(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (long?)element;
			}
			return null;
		}
		public static decimal GetDecimal(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (decimal)el.GetElement(name);
		}
		public static decimal GetDecimalOrDefault(this XElement el, string name)
		{
			Func<XElement, decimal> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__13_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__13_0 = new Func<XElement, decimal>(XLinqExtensions.<>c.<>9.<GetDecimalOrDefault>b__13_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static decimal? GetNullableDecimal(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (decimal?)element;
			}
			return null;
		}
		public static DateTime GetDateTime(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (DateTime)el.GetElement(name);
		}
		public static DateTime GetDateTimeOrDefault(this XElement el, string name)
		{
			Func<XElement, DateTime> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__16_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__16_0 = new Func<XElement, DateTime>(XLinqExtensions.<>c.<>9.<GetDateTimeOrDefault>b__16_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static DateTime? GetNullableDateTime(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (DateTime?)element;
			}
			return null;
		}
		public static TimeSpan GetTimeSpan(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (TimeSpan)el.GetElement(name);
		}
		public static TimeSpan GetTimeSpanOrDefault(this XElement el, string name)
		{
			Func<XElement, TimeSpan> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__19_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__19_0 = new Func<XElement, TimeSpan>(XLinqExtensions.<>c.<>9.<GetTimeSpanOrDefault>b__19_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static TimeSpan? GetNullableTimeSpan(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (TimeSpan?)element;
			}
			return null;
		}
		public static Guid GetGuid(this XElement el, string name)
		{
			el.AssertElementHasValue(name);
			return (Guid)el.GetElement(name);
		}
		public static Guid GetGuidOrDefault(this XElement el, string name)
		{
			Func<XElement, Guid> arg_21_2;
			if ((arg_21_2 = XLinqExtensions.<>c.<>9__22_0) == null)
			{
				arg_21_2 = (XLinqExtensions.<>c.<>9__22_0 = new Func<XElement, Guid>(XLinqExtensions.<>c.<>9.<GetGuidOrDefault>b__22_0));
			}
			return el.GetElementValueOrDefault(name, arg_21_2);
		}
		public static Guid? GetNullableGuid(this XElement el, string name)
		{
			XElement element = el.GetElement(name);
			if (element != null && !string.IsNullOrEmpty(element.Value))
			{
				return (Guid?)element;
			}
			return null;
		}
		public static T GetElementValueOrDefault<T>(this XElement element, string name, Func<XElement, T> converter)
		{
			if (converter == null)
			{
				throw new ArgumentNullException("converter");
			}
			XElement element2 = element.GetElement(name);
			if (element2 != null && !string.IsNullOrEmpty(element2.Value))
			{
				return converter(element2);
			}
			return default(T);
		}
		public static XElement GetElement(this XElement element, string name)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			return element.AnyElement(name);
		}
		public static void AssertElementHasValue(this XElement element, string name)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			XElement xElement = element.AnyElement(name);
			if (xElement == null || string.IsNullOrEmpty(xElement.Value))
			{
				throw new ArgumentNullException(name, string.Format("{0} is required", name));
			}
		}
		public static List<string> GetValues(this IEnumerable<XElement> els)
		{
			List<string> list = new List<string>();
			foreach (XElement current in els)
			{
				list.Add(current.Value);
			}
			return list;
		}
		public static XAttribute AnyAttribute(this XElement element, string name)
		{
			if (element == null)
			{
				return null;
			}
			foreach (XAttribute current in element.Attributes())
			{
				if (current.Name.LocalName == name)
				{
					return current;
				}
			}
			return null;
		}
		public static IEnumerable<XElement> AllElements(this XElement element, string name)
		{
			List<XElement> list = new List<XElement>();
			if (element == null)
			{
				return list;
			}
			foreach (XNode current in element.Nodes())
			{
				if (current.NodeType == XmlNodeType.Element)
				{
					XElement xElement = (XElement)current;
					if (xElement.Name.LocalName == name)
					{
						list.Add(xElement);
					}
				}
			}
			return list;
		}
		public static XElement AnyElement(this XElement element, string name)
		{
			if (element == null)
			{
				return null;
			}
			foreach (XNode current in element.Nodes())
			{
				if (current.NodeType == XmlNodeType.Element)
				{
					XElement xElement = (XElement)current;
					if (xElement.Name.LocalName == name)
					{
						return xElement;
					}
				}
			}
			return null;
		}
		public static XElement AnyElement(this IEnumerable<XElement> elements, string name)
		{
			foreach (XElement current in elements)
			{
				if (current.Name.LocalName == name)
				{
					return current;
				}
			}
			return null;
		}
		public static IEnumerable<XElement> AllElements(this IEnumerable<XElement> elements, string name)
		{
			List<XElement> list = new List<XElement>();
			foreach (XElement current in elements)
			{
				list.AddRange(current.AllElements(name));
			}
			return list;
		}
		public static XElement FirstElement(this XElement element)
		{
			if (element.FirstNode.NodeType == XmlNodeType.Element)
			{
				return (XElement)element.FirstNode;
			}
			return null;
		}
	}
}
